package com.dys;

import com.dys.dao.AccountDAO;
import com.dys.dao.PersonDAO;
import com.dys.dao.impl.AccountDAOImpl02;
import com.dys.dao.impl.PersonDaoImpl02;
import com.dys.entity.Account;
import com.dys.entity.Person;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.SQLException;
import java.util.List;

/**
 * @className: com.dys.DaoUtilsTest
 * @description: com.dys
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/09/13 下午11:36
 */
public class DaoUtilsTest {
    /**
     * 这是一个main()方法,是应用程序的入口
     *
     * @param args 方法的形参列表,是一个字符串数组,包含了从命令行传递给程序的参数
     */
    public static void main(String[] args) throws SQLException {
        testDelete();
    }

    /**
     * 测试添加
     *
     * @throws SQLException
     */
    public static void testAdd() throws SQLException {
        PersonDAO personDAO = new PersonDaoImpl02();
        Person person = new Person();
        person.setName("刘亦菲");
        person.setAge(28);
        person.setEmail("123@qq.com");
        person.setDateOfBirth(new Date(System.currentTimeMillis()));
        person.setAddress("广东深圳龙岗区布吉街道");
        int count = personDAO.insert(person);
        System.out.println("受影响行数: " + count);
    }

    /**
     * 测试修改
     *
     * @throws SQLException
     */
    public static void testUpdate() throws SQLException {
        PersonDAO personDAO = new PersonDaoImpl02();
        Person person = new Person();
        person.setId(1);
        person.setName("李沁");
        person.setAge(25);
        int count = personDAO.updateById(person);
        System.out.println("受影响行数: " + count);
    }

    /**
     * 测试删除功能
     *
     * @throws SQLException
     */
    public static void testDelete() throws SQLException {
        PersonDAO personDAO = new PersonDaoImpl02();
        int count = personDAO.deleteById(12);
        System.out.println("受影响行数: " + count);
    }

    /**
     * 测试查询单个对象
     *
     * @throws SQLException
     */
    public static void testSelect() throws SQLException {
        PersonDAO personDAO = new PersonDaoImpl02();
        Person person = personDAO.findById(1);
        System.out.println("查询结果：" + person);
    }

    /**
     * 测试查询所有对象
     *
     * @throws SQLException
     */
    public static void testSelectAll() throws SQLException {
        PersonDAO personDAO = new PersonDaoImpl02();
        List<Person> persons = personDAO.findAll();
        persons.stream().forEach(System.out::println);
    }

    /**
     * 测试查询单个账号
     *
     * @throws SQLException
     */
    public static void testAccountSelect() throws SQLException {
        AccountDAO accountDAO = new AccountDAOImpl02();
        Account account = accountDAO.findAccountByCardNo("1001");
        System.out.println("账号信息：" + account);
    }

    /**
     * 测试修改账号
     *
     * @throws SQLException
     */
    public static void testAccountUpdate() throws SQLException {
        AccountDAO accountDAO = new AccountDAOImpl02();
        Account account = new Account();
        account.setCardNo("1001");
        account.setMoney(new BigDecimal("100"));
        int count = accountDAO.updateAccountByCardNo(account);
        System.out.println("受影响行数：" + count);
    }
}
